<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover"><title>爬虫一：简单静态网页的爬取 | 有求必应</title><meta name="author" content="Mr.wu"><meta name="copyright" content="Mr.wu"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="基本思想利用Request库发起请求，得到响应数据；利用BeautifulSoup解析数据，进而获取页面标签中我们所需要的数据 使用条件爬取网页内容静态呈现 使用技术介绍Request库 安装1pip3 install requests 检验安装成功12python import requests 查询结果如下  常用方法     方法 作用    rquest() 最基本的方法，是之后应对不同请">
<meta property="og:type" content="article">
<meta property="og:title" content="爬虫一：简单静态网页的爬取">
<meta property="og:url" content="https://comortgfytry.github.io/pages/%E7%88%AC%E8%99%AB/%E7%88%AC%E8%99%AB%E4%B8%80%EF%BC%9A%E7%AE%80%E5%8D%95%E9%9D%99%E6%80%81%E7%BD%91%E9%A1%B5%E7%9A%84%E7%88%AC%E5%8F%96.html">
<meta property="og:site_name" content="有求必应">
<meta property="og:description" content="基本思想利用Request库发起请求，得到响应数据；利用BeautifulSoup解析数据，进而获取页面标签中我们所需要的数据 使用条件爬取网页内容静态呈现 使用技术介绍Request库 安装1pip3 install requests 检验安装成功12python import requests 查询结果如下  常用方法     方法 作用    rquest() 最基本的方法，是之后应对不同请">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://comortgfytry.github.io/img/avatar.jpg">
<meta property="article:published_time" content="2023-07-21T15:00:00.000Z">
<meta property="article:modified_time" content="2023-07-23T15:24:07.825Z">
<meta property="article:author" content="Mr.wu">
<meta property="article:tag" content="Python">
<meta property="article:tag" content="爬虫">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://comortgfytry.github.io/img/avatar.jpg"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="https://comortgfytry.github.io/pages/%E7%88%AC%E8%99%AB/%E7%88%AC%E8%99%AB%E4%B8%80%EF%BC%9A%E7%AE%80%E5%8D%95%E9%9D%99%E6%80%81%E7%BD%91%E9%A1%B5%E7%9A%84%E7%88%AC%E5%8F%96.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = {
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","preload":true,"top_n_per_article":1,"unescape":false,"languages":{"hits_empty":"找不到您查询的内容：${query}","hits_stats":"共找到 ${hits} 篇文章"}},
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":300},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  dateSuffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: {"limitCount":200,"languages":{"author":"作者: Mr.wu","link":"链接: ","source":"来源: 有求必应","info":"著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"}},
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: true,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: false,
  },
  autoDarkmode: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '爬虫一：简单静态网页的爬取',
  isPost: true,
  isHome: false,
  isHighlightShrink: undefined,
  isToc: true,
  postUpdate: '2023-07-23 23:24:07'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
    win.getCSS = (url,id = false) => new Promise((resolve, reject) => {
      const link = document.createElement('link')
      link.rel = 'stylesheet'
      link.href = url
      if (id) link.id = id
      link.onerror = reject
      link.onload = link.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        link.onload = link.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(link)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="/css/mycss.css"><style type="text/css"> #universe {  display: block; position: fixed; margin: 0; padding: 0; border: 0; outline: 0; left: 0; top: 0; width: 100%; height: 100%; pointer-events: none; z-index: 1; } </style><meta name="generator" content="Hexo 6.3.0"></head><body><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src= "" data-lazy-src="/img/avatar.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">63</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">32</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">25</div></a></div><hr class="custom-hr"/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-bookmark"></i><span> 博文</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/categories/"><i class="fa-fw fa fa-archive"></i><span> 分类</span></a></li><li><a class="site-page child" href="/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/archives/"><i class="fa-fw fa fa-folder-open"></i><span> 归档</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/comment/"><i class="fa-fw fa fa-paper-plane"></i><span> 留言板</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-user"></i><span> 关于我</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://api.aqcoder.cn/random')"><nav id="nav"><span id="blog-info"><a href="/" title="有求必应"><span class="site-name">有求必应</span></a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search" href="javascript:void(0);"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-bookmark"></i><span> 博文</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/categories/"><i class="fa-fw fa fa-archive"></i><span> 分类</span></a></li><li><a class="site-page child" href="/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/archives/"><i class="fa-fw fa fa-folder-open"></i><span> 归档</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/comment/"><i class="fa-fw fa fa-paper-plane"></i><span> 留言板</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-user"></i><span> 关于我</span></a></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">爬虫一：简单静态网页的爬取</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2023-07-21T15:00:00.000Z" title="发表于 2023-07-21 23:00:00">2023-07-21</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2023-07-23T15:24:07.825Z" title="更新于 2023-07-23 23:24:07">2023-07-23</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E7%88%AC%E8%99%AB/">爬虫</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">1.7k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>7分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="爬虫一：简单静态网页的爬取"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="基本思想"><a href="#基本思想" class="headerlink" title="基本思想"></a>基本思想</h1><p>利用Request库发起请求，得到响应数据；利用BeautifulSoup解析数据，进而获取页面标签中我们所需要的数据</p>
<h1 id="使用条件"><a href="#使用条件" class="headerlink" title="使用条件"></a>使用条件</h1><p>爬取网页内容静态呈现</p>
<h1 id="使用技术介绍"><a href="#使用技术介绍" class="headerlink" title="使用技术介绍"></a>使用技术介绍</h1><h2 id="Request库"><a href="#Request库" class="headerlink" title="Request库"></a>Request库</h2><ul>
<li>安装<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">pip3 install requests</span><br></pre></td></tr></table></figure></li>
<li>检验安装成功<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">python </span><br><span class="line">import requests</span><br></pre></td></tr></table></figure>
查询结果如下<br><img src= "" data-lazy-src="/pages/%E7%88%AC%E8%99%AB/%E7%88%AC%E8%99%AB%E4%B8%80%EF%BC%9A%E7%AE%80%E5%8D%95%E9%9D%99%E6%80%81%E7%BD%91%E9%A1%B5%E7%9A%84%E7%88%AC%E5%8F%96/image.png" alt="image"> </li>
<li>常用方法</li>
</ul>
<table>
<thead>
<tr>
<th align="left">方法</th>
<th align="left">作用</th>
</tr>
</thead>
<tbody><tr>
<td align="left">rquest()</td>
<td align="left"><div>最基本的方法，是之后应对不同请求方法的基本方法</div><div>用get()举例：</div><div><code>def get(url, params=None, **kwargs):return request(&quot;get&quot;, url, params=params, **kwargs)</code></div></td>
</tr>
<tr>
<td align="left">get()</td>
<td align="left">对应http中get方法</td>
</tr>
<tr>
<td align="left">post()</td>
<td align="left">对应http中post方法</td>
</tr>
<tr>
<td align="left">head()</td>
<td align="left">对应http中head方法</td>
</tr>
<tr>
<td align="left">put()</td>
<td align="left">对应http中put方法</td>
</tr>
<tr>
<td align="left">patch()</td>
<td align="left">对应http中patch方法</td>
</tr>
<tr>
<td align="left">get()</td>
<td align="left">对应http中delete方法</td>
</tr>
<tr>
<td align="left"></td>
<td align="left"></td>
</tr>
</tbody></table>
<p>传入参数：</p>
<table>
<thead>
<tr>
<th align="left">request()</th>
</tr>
</thead>
<tbody><tr>
<td align="left">method:http请求类型（<code>GET</code>, <code>OPTIONS</code>, <code>HEAD</code>, <code>POST</code>, <code>PUT</code>, <code>PATCH</code>, or <code>DELETE</code>）</td>
</tr>
<tr>
<td align="left">url:网页链接</td>
</tr>
<tr>
<td align="left">header:请求头，模拟正常请求时会发送的一些必要内容（<strong>详情见注解1</strong>）</td>
</tr>
<tr>
<td align="left">proxies:用来设置代理服务器（<strong>详情见注解2</strong>）</td>
</tr>
<tr>
<td align="left">timeout：设置超时（<strong>详情见注解3</strong>）</td>
</tr>
<tr>
<td align="left"></td>
</tr>
</tbody></table>
<p>其他方法以request()为支撑省略了method，其他参数一样</p>
<blockquote>
<p>注解</p>
</blockquote>
<blockquote>
<p>注解1：网页请求报文模板如下:<br><img src= "" data-lazy-src="/pages/%E7%88%AC%E8%99%AB/%E7%88%AC%E8%99%AB%E4%B8%80%EF%BC%9A%E7%AE%80%E5%8D%95%E9%9D%99%E6%80%81%E7%BD%91%E9%A1%B5%E7%9A%84%E7%88%AC%E5%8F%96/image1.png" alt="image1"><br>将其中数据成用字典封装  </p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">&gt;headers = &#123;</span><br><span class="line">       <span class="string">&quot;user-agent&quot;</span>: <span class="string">&quot;Mozilla/5.0 (Windows NT 10.0; WOW64)AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.9 Safari/537.36&quot;</span>    &#125;</span><br></pre></td></tr></table></figure>
<p>一般说添加<strong>user-agent</strong>就可以了  </p>
</blockquote>
<blockquote>
<p>注解2：可以用proxies来切换发送请求的ip</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">proxies = &#123; <span class="string">&quot;http&quot;</span>: <span class="string">&quot;http://10.10.1.10:3128&quot;</span>, <span class="string">&quot;https&quot;</span>: <span class="string">&quot;http://10.10.1.10:1080&quot;</span>, &#125;</span><br></pre></td></tr></table></figure>
<p>这样做的好处是不会因为爬虫频繁请求网页导致网站限制ip访问</p>
</blockquote>
<blockquote>
<p>注解3：用于限制接受响应的最大时间，超出则会报错，默认为不限时<br>也可以传入一个元组<code>r = requests.get(&#39;https://www.taobao.com&#39;,timeout=(5,30))</code><br>分别代表连接时间和读取时间</p>
</blockquote>
<h2 id="BeautifulSoup库"><a href="#BeautifulSoup库" class="headerlink" title="BeautifulSoup库"></a>BeautifulSoup库</h2><ul>
<li>选择解析器解析指定内容</li>
</ul>
<p>基本格式：<code>soup = BeautifulSoup(解析内容, 解析器)</code></p>
<p>常用解析器：lxml, html(默认), xml…</p>
<blockquote>
<p>注意：如果是标准正确的html内容，不同解析器解析结果相同；反之，会有不同</p>
</blockquote>
<ul>
<li>处理解析数据的方法</li>
</ul>
<table>
<thead>
<tr>
<th align="left">方法</th>
<th align="left">参数</th>
<th align="left">说明</th>
</tr>
</thead>
<tbody><tr>
<td align="left">find()</td>
<td align="left">name,class_</td>
<td align="left">查找HTML中第一个为类型为name，class为class_的标签</td>
</tr>
<tr>
<td align="left">find_all()</td>
<td align="left">name,class_</td>
<td align="left">返回装有所有符合name，class的标签列表</td>
</tr>
<tr>
<td align="left">text</td>
<td align="left">~</td>
<td align="left">获取标签中内容</td>
</tr>
</tbody></table>
<ul>
<li>网页中定位所要查找的内容</li>
</ul>
<p>F12开发者工具 + 选中网页内容，右键检查</p>
<h1 id="实战使用"><a href="#实战使用" class="headerlink" title="实战使用"></a>实战使用</h1><ul>
<li><p>目标：爬取<strong>去哪儿旅行网</strong>的不同省份的热门城市和景点数据（<a target="_blank" rel="noopener" href="http://travel.qunar.com/p-sf297555-%7B%E7%9C%81%E4%BB%BD%E7%9A%84%E6%8B%BC%E9%9F%B3%E5%B0%8F%E5%86%99%7D%EF%BC%89">http://travel.qunar.com/p-sf297555-{省份的拼音小写}）</a></p>
</li>
<li><p>输入省份，获取该省的页面，爬取热门城市</p>
</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">def</span> <span class="title function_">get_soup_province</span>():</span><br><span class="line">    url = <span class="built_in">input</span>(<span class="string">&#x27;请输入要查询省份的去哪儿旅行网站链接：\n&#x27;</span>)</span><br><span class="line">    <span class="comment"># 请求头 浏览器类型</span></span><br><span class="line">    headers = &#123;</span><br><span class="line">        <span class="string">&quot;user-agent&quot;</span>: <span class="string">&quot;Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.9 Safari/537.36&quot;</span></span><br><span class="line">    &#125;</span><br><span class="line">    response = requests.get(url, headers=headers);</span><br><span class="line">    html = response.text</span><br><span class="line">    soup = BeautifulSoup(html, <span class="string">&#x27;lxml&#x27;</span>)</span><br><span class="line">    <span class="keyword">return</span> soup</span><br><span class="line"></span><br><span class="line">province =  <span class="built_in">input</span>(<span class="string">&#x27;请输入省份：\n&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 爬取输入省份的主要城市的url（每个省6个）</span></span><br><span class="line">soup = get_soup_province()</span><br><span class="line">cityMap = <span class="built_in">dict</span>()</span><br><span class="line"><span class="keyword">for</span> li <span class="keyword">in</span> soup.find_all(name=<span class="string">&#x27;li&#x27;</span>, class_=<span class="string">&#x27;sub_item&#x27;</span>):</span><br><span class="line">    city = li.a[<span class="string">&quot;title&quot;</span>]</span><br><span class="line">    url = li.a[<span class="string">&quot;href&quot;</span>]</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> <span class="string">&#x27;旅游景点&#x27;</span> <span class="keyword">in</span> city:</span><br><span class="line">        cityMap[city] = url</span><br></pre></td></tr></table></figure>
<p>此时该省份下的最热门6个城市及其页面链接已经存入cityMap字典中</p>
<ul>
<li>继续爬取6个热门城市的页面，获取其各自的前30景区信息</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line">city = [] <span class="comment"># 景区归属城市</span></span><br><span class="line">spotName = [] <span class="comment"># 景点名称</span></span><br><span class="line">score = []  <span class="comment"># 景点满意度</span></span><br><span class="line">ranking = []  <span class="comment"># 排名</span></span><br><span class="line">comments = [] <span class="comment"># 简介</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> key <span class="keyword">in</span> cityMap:</span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">1</span>, <span class="number">4</span>):   <span class="comment"># 每个城市爬取前30的景区   </span></span><br><span class="line">        <span class="comment"># url拼接：到达城市景点不同页数 http://travel.qunar.com/p-cs299782-xiamen-jingdian-1-2</span></span><br><span class="line">        tmp = cityMap[key] + <span class="string">&#x27;-1-&#x27;</span></span><br><span class="line">        url = tmp + <span class="built_in">str</span>(i) <span class="comment"># i 可以说代表页数</span></span><br><span class="line">        soup = get_soup_url(url)</span><br><span class="line"></span><br><span class="line">        <span class="comment"># 景点名称</span></span><br><span class="line">        <span class="keyword">for</span> span <span class="keyword">in</span> soup.find_all(name=<span class="string">&#x27;span&#x27;</span>, class_=<span class="string">&#x27;cn_tit&#x27;</span>):</span><br><span class="line">            city.append(key)</span><br><span class="line">            spotName.append(span.text)</span><br><span class="line">        <span class="built_in">print</span>(spotName)</span><br><span class="line"></span><br><span class="line">        <span class="comment"># 评分（满意度）</span></span><br><span class="line">        <span class="keyword">for</span> span <span class="keyword">in</span> soup.find_all(name=<span class="string">&#x27;span&#x27;</span>, class_=<span class="string">&#x27;cur_star&#x27;</span>):</span><br><span class="line">                percent = span[<span class="string">&#x27;style&#x27;</span>]</span><br><span class="line">                tmp = <span class="built_in">str</span>(percent).split(<span class="string">&#x27;:&#x27;</span>)[<span class="number">1</span>]</span><br><span class="line">                score.append(tmp)</span><br><span class="line">        <span class="built_in">print</span>(score)</span><br><span class="line"></span><br><span class="line">        <span class="comment"># 排名</span></span><br><span class="line">        <span class="keyword">for</span> span <span class="keyword">in</span> soup.find_all(name=<span class="string">&#x27;span&#x27;</span>, class_=<span class="string">&#x27;ranking_sum&#x27;</span>):</span><br><span class="line">                ranking.append(span.text)</span><br><span class="line">        <span class="built_in">print</span>(ranking)</span><br><span class="line"></span><br><span class="line">        <span class="comment"># 简介</span></span><br><span class="line">        <span class="keyword">for</span> div <span class="keyword">in</span> soup.find_all(name=<span class="string">&#x27;div&#x27;</span>, class_=<span class="string">&#x27;desbox&#x27;</span>):</span><br><span class="line">                comments.append(div.text)</span><br><span class="line">        <span class="built_in">print</span>(comments)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 爬取每个景点需要的信息</span></span><br><span class="line"><span class="built_in">map</span> = &#123;</span><br><span class="line">    <span class="string">&#x27;所在城市&#x27;</span>:city,</span><br><span class="line">    <span class="string">&#x27;景点名称&#x27;</span>: spotName,</span><br><span class="line">    <span class="string">&#x27;满意度&#x27;</span>: score,</span><br><span class="line">    <span class="string">&#x27;排名&#x27;</span>: ranking,</span><br><span class="line">    <span class="string">&#x27;简介&#x27;</span>: comments</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>此时该省景区信息已经存入map字典中</p>
<ul>
<li>存入数据库或者excel表格</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># excel表格</span></span><br><span class="line">df = pd.DataFrame(<span class="built_in">map</span>)</span><br><span class="line">df.to_excel(province + <span class="string">&quot;.xlsx&quot;</span>, index=<span class="literal">False</span>)</span><br></pre></td></tr></table></figure>

<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 数据库</span></span><br><span class="line"><span class="comment"># 数据库连接</span></span><br><span class="line">conn = pymysql.connect(</span><br><span class="line">    host=<span class="string">&#x27;127.0.0.1&#x27;</span>,</span><br><span class="line">    user=<span class="string">&#x27;root&#x27;</span>,</span><br><span class="line">    password=<span class="string">&#x27;1234&#x27;</span>,</span><br><span class="line">    port=<span class="number">3306</span>,</span><br><span class="line">    database=<span class="string">&#x27;mw&#x27;</span>)</span><br><span class="line">cursor = conn.cursor()</span><br><span class="line"></span><br><span class="line"><span class="comment"># 保存到数据库</span></span><br><span class="line">sql = <span class="string">&quot;insert into tour (city, spotName, score, ranking, comments) values(%s，%s，%s，%s，%s)&quot;</span></span><br><span class="line"><span class="keyword">for</span> lt <span class="keyword">in</span> lst:</span><br><span class="line">    col1 = city.pop()</span><br><span class="line">    col2 = spotName.pop()</span><br><span class="line">    col3 = score.pop()</span><br><span class="line">    col4 = ranking.pop()</span><br><span class="line">    col5 = comments.pop()</span><br><span class="line">    insert = cursor.execute(sql, (col1, col2, col3, col4, col5))</span><br><span class="line"></span><br><span class="line">cursor.close()</span><br><span class="line">conn.commit()</span><br><span class="line"><span class="comment"># 关闭连接</span></span><br><span class="line">conn.close()</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<ul>
<li>完整代码  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> requests</span><br><span class="line"></span><br><span class="line"><span class="keyword">from</span> bs4 <span class="keyword">import</span> BeautifulSoup</span><br><span class="line"><span class="keyword">import</span> pandas <span class="keyword">as</span> pd</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># &#x27;https://travel.qunar.com/p-sf298098-fujian&#x27;</span></span><br><span class="line"><span class="comment"># 爬取省份下城市soup</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">get_soup_province</span>():</span><br><span class="line">    url = <span class="built_in">input</span>(<span class="string">&#x27;请输入要查询省份的去哪儿旅行网站链接：\n&#x27;</span>)</span><br><span class="line">    <span class="comment"># 请求头 浏览器类型</span></span><br><span class="line">    headers = &#123;</span><br><span class="line">        <span class="string">&quot;user-agent&quot;</span>: <span class="string">&quot;Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.9 Safari/537.36&quot;</span></span><br><span class="line">    &#125;</span><br><span class="line">    response = requests.get(url, headers=headers);</span><br><span class="line">    html = response.text</span><br><span class="line">    soup = BeautifulSoup(html, <span class="string">&#x27;lxml&#x27;</span>)</span><br><span class="line">    <span class="keyword">return</span> soup</span><br><span class="line"></span><br><span class="line"><span class="comment"># 爬取城市下景点soup</span></span><br><span class="line"><span class="comment"># 爬取景点内容soup</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">get_soup_url</span>(<span class="params">url</span>):</span><br><span class="line">    <span class="comment"># 请求头 浏览器类型</span></span><br><span class="line">    headers = &#123;</span><br><span class="line">        <span class="string">&quot;user-agent&quot;</span>: <span class="string">&quot;Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.9 Safari/537.36&quot;</span></span><br><span class="line">    &#125;</span><br><span class="line">    response = requests.get(url, headers=headers);</span><br><span class="line">    html = response.text</span><br><span class="line">    soup = BeautifulSoup(html, <span class="string">&#x27;lxml&#x27;</span>)</span><br><span class="line">    <span class="keyword">return</span> soup</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">province =  <span class="built_in">input</span>(<span class="string">&#x27;请输入省份：\n&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 爬取输入省份的主要城市的url（每个省6个）</span></span><br><span class="line">soup = get_soup_province()</span><br><span class="line">cityMap = <span class="built_in">dict</span>()</span><br><span class="line"><span class="keyword">for</span> li <span class="keyword">in</span> soup.find_all(name=<span class="string">&#x27;li&#x27;</span>, class_=<span class="string">&#x27;sub_item&#x27;</span>):</span><br><span class="line">    city = li.a[<span class="string">&quot;title&quot;</span>]</span><br><span class="line">    url = li.a[<span class="string">&quot;href&quot;</span>]</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> <span class="string">&#x27;旅游景点&#x27;</span> <span class="keyword">in</span> city:</span><br><span class="line">        cityMap[city] = url</span><br><span class="line"><span class="comment"># print(cityMap)</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 爬取各城市的景点链接（按人气排名前百的城市）</span></span><br><span class="line">city = []</span><br><span class="line">spotName = []</span><br><span class="line">score = []</span><br><span class="line">ranking = []</span><br><span class="line">comments = []</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> key <span class="keyword">in</span> cityMap:</span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">1</span>, <span class="number">4</span>):   </span><br><span class="line">        <span class="comment"># url拼接：到达城市景点不同页数 http://travel.qunar.com/p-cs299782-xiamen-jingdian-1-2</span></span><br><span class="line">        tmp = cityMap[key] + <span class="string">&#x27;-1-&#x27;</span></span><br><span class="line">        url = tmp + <span class="built_in">str</span>(i)</span><br><span class="line">        soup = get_soup_url(url)</span><br><span class="line"></span><br><span class="line">        <span class="comment"># 景点名称</span></span><br><span class="line">        <span class="keyword">for</span> span <span class="keyword">in</span> soup.find_all(name=<span class="string">&#x27;span&#x27;</span>, class_=<span class="string">&#x27;cn_tit&#x27;</span>):</span><br><span class="line">            city.append(key)</span><br><span class="line">            spotName.append(span.text)</span><br><span class="line">        <span class="built_in">print</span>(spotName)</span><br><span class="line"></span><br><span class="line">        <span class="comment"># 评分（满意度）</span></span><br><span class="line">        <span class="keyword">for</span> span <span class="keyword">in</span> soup.find_all(name=<span class="string">&#x27;span&#x27;</span>, class_=<span class="string">&#x27;cur_star&#x27;</span>):</span><br><span class="line">                percent = span[<span class="string">&#x27;style&#x27;</span>]</span><br><span class="line">                tmp = <span class="built_in">str</span>(percent).split(<span class="string">&#x27;:&#x27;</span>)[<span class="number">1</span>]</span><br><span class="line">                score.append(tmp)</span><br><span class="line">        <span class="built_in">print</span>(score)</span><br><span class="line"></span><br><span class="line">        <span class="comment"># 排名</span></span><br><span class="line">        <span class="keyword">for</span> span <span class="keyword">in</span> soup.find_all(name=<span class="string">&#x27;span&#x27;</span>, class_=<span class="string">&#x27;ranking_sum&#x27;</span>):</span><br><span class="line">                ranking.append(span.text)</span><br><span class="line">        <span class="built_in">print</span>(ranking)</span><br><span class="line"></span><br><span class="line">        <span class="comment"># 简介</span></span><br><span class="line">        <span class="keyword">for</span> div <span class="keyword">in</span> soup.find_all(name=<span class="string">&#x27;div&#x27;</span>, class_=<span class="string">&#x27;desbox&#x27;</span>):</span><br><span class="line">                comments.append(div.text)</span><br><span class="line">        <span class="built_in">print</span>(comments)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 爬取每个景点需要的信息</span></span><br><span class="line"><span class="built_in">map</span> = &#123;</span><br><span class="line">    <span class="string">&#x27;所在城市&#x27;</span>:city,</span><br><span class="line">    <span class="string">&#x27;景点名称&#x27;</span>: spotName,</span><br><span class="line">    <span class="string">&#x27;满意度&#x27;</span>: score,</span><br><span class="line">    <span class="string">&#x27;排名&#x27;</span>: ranking,</span><br><span class="line">    <span class="string">&#x27;简介&#x27;</span>: comments</span><br><span class="line">&#125;</span><br><span class="line">df = pd.DataFrame(<span class="built_in">map</span>)</span><br><span class="line">df.to_excel(province + <span class="string">&quot;.xlsx&quot;</span>, index=<span class="literal">False</span>)</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="https://comortgfytry.github.io">Mr.wu</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://comortgfytry.github.io/pages/%E7%88%AC%E8%99%AB/%E7%88%AC%E8%99%AB%E4%B8%80%EF%BC%9A%E7%AE%80%E5%8D%95%E9%9D%99%E6%80%81%E7%BD%91%E9%A1%B5%E7%9A%84%E7%88%AC%E5%8F%96.html">https://comortgfytry.github.io/pages/%E7%88%AC%E8%99%AB/%E7%88%AC%E8%99%AB%E4%B8%80%EF%BC%9A%E7%AE%80%E5%8D%95%E9%9D%99%E6%80%81%E7%BD%91%E9%A1%B5%E7%9A%84%E7%88%AC%E5%8F%96.html</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://comortgfytry.github.io" target="_blank">有求必应</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/Python/">Python</a><a class="post-meta__tags" href="/tags/%E7%88%AC%E8%99%AB/">爬虫</a></div><div class="post_share"><div class="social-share" data-image="/img/avatar.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/pages/%E7%88%AC%E8%99%AB/%E5%88%86%E5%B8%83%E5%BC%8F%E7%88%AC%E8%99%AB.html" title="分布式爬虫"><div class="cover" style="background: var(--default-bg-color)"></div><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">分布式爬虫</div></div></a></div><div class="next-post pull-right"><a href="/pages/%E7%88%AC%E8%99%AB/%E7%88%AC%E8%99%AB%E4%BA%8C%EF%BC%9A%E5%8A%A8%E6%80%81%E7%BD%91%E9%A1%B5%E7%9A%84%E7%88%AC%E5%8F%96.html" title="爬虫二：动态网页的爬取"><div class="cover" style="background: var(--default-bg-color)"></div><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">爬虫二：动态网页的爬取</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/pages/%E7%88%AC%E8%99%AB/%E5%88%86%E5%B8%83%E5%BC%8F%E7%88%AC%E8%99%AB.html" title="分布式爬虫"><div class="cover" style="background: var(--default-bg-color)"></div><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-07-19</div><div class="title">分布式爬虫</div></div></a></div><div><a href="/pages/%E7%88%AC%E8%99%AB/%E7%88%AC%E8%99%AB%E4%BA%8C%EF%BC%9A%E5%8A%A8%E6%80%81%E7%BD%91%E9%A1%B5%E7%9A%84%E7%88%AC%E5%8F%96.html" title="爬虫二：动态网页的爬取"><div class="cover" style="background: var(--default-bg-color)"></div><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-07-22</div><div class="title">爬虫二：动态网页的爬取</div></div></a></div><div><a href="/pages/%E7%88%AC%E8%99%AB/%E7%88%AC%E8%99%AB%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98%E6%80%BB%E7%BB%93.html" title="爬虫常见问题总结"><div class="cover" style="background: var(--default-bg-color)"></div><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-07-25</div><div class="title">爬虫常见问题总结</div></div></a></div><div><a href="/pages/flask%E6%A1%86%E6%9E%B6/flask%E5%85%A5%E9%97%A8%E4%BD%BF%E7%94%A8.html" title="flask入门使用"><div class="cover" style="background: var(--default-bg-color)"></div><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-07-24</div><div class="title">flask入门使用</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src= "" data-lazy-src="/img/avatar.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">Mr.wu</div><div class="author-info__description">分享知识</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">63</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">32</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">25</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/xxxxxx"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/xxxxxx" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="https://blog.csdn.net/blue_gym" target="_blank" title="CSDN"><i class="fa fa-book-open"></i></a><a class="social-icon" href="mailto:shouwangyinying@163.com" target="_blank" title="Email"><i class="fas fa-envelope-open-text"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">Nice to meet you. please get website connection by Ctrl + F5</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%9F%BA%E6%9C%AC%E6%80%9D%E6%83%B3"><span class="toc-number">1.</span> <span class="toc-text">基本思想</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8%E6%9D%A1%E4%BB%B6"><span class="toc-number">2.</span> <span class="toc-text">使用条件</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8%E6%8A%80%E6%9C%AF%E4%BB%8B%E7%BB%8D"><span class="toc-number">3.</span> <span class="toc-text">使用技术介绍</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Request%E5%BA%93"><span class="toc-number">3.1.</span> <span class="toc-text">Request库</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#BeautifulSoup%E5%BA%93"><span class="toc-number">3.2.</span> <span class="toc-text">BeautifulSoup库</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%AE%9E%E6%88%98%E4%BD%BF%E7%94%A8"><span class="toc-number">4.</span> <span class="toc-text">实战使用</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/pages/%E7%AE%97%E6%B3%95/%E6%99%AE%E9%80%9A%E6%95%B0%E7%BB%84/%E7%BC%BA%E5%A4%B1%E7%9A%84%E7%AC%AC%E4%B8%80%E4%B8%AA%E6%AD%A3%E6%95%B0.html" title="缺失的第一个正数">缺失的第一个正数</a><time datetime="2024-01-06T13:48:58.072Z" title="发表于 2024-01-06 21:48:58">2024-01-06</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/pages/%E7%AE%97%E6%B3%95/%E7%9F%A9%E9%98%B5/%E7%9F%A9%E9%98%B5%E7%BD%AE%E9%9B%B6.html" title="矩阵置零">矩阵置零</a><time datetime="2024-01-06T13:48:58.070Z" title="发表于 2024-01-06 21:48:58">2024-01-06</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/pages/RPC%E6%A1%86%E6%9E%B6/%E6%9C%8D%E5%8A%A1%E8%BF%9C%E7%A8%8B%E8%B0%83%E7%94%A8(RestTemplate%E3%80%81Feign%E3%80%81Dubbo).html" title="服务远程调用(RestTemplate、Feign、Dubbo)">服务远程调用(RestTemplate、Feign、Dubbo)</a><time datetime="2023-12-30T02:24:00.000Z" title="发表于 2023-12-30 10:24:00">2023-12-30</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/pages/%E8%BD%AF%E4%BB%B6%E6%9E%B6%E6%9E%84/%E8%BD%AF%E4%BB%B6%E6%9E%B6%E6%9E%84%E5%A4%8D%E4%B9%A0.html" title="软件架构复习">软件架构复习</a><time datetime="2023-12-23T15:50:20.000Z" title="发表于 2023-12-23 23:50:20">2023-12-23</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/pages/%E7%AE%97%E6%B3%95/%E4%BA%8C%E5%88%86%E6%9F%A5%E6%89%BE/%E6%90%9C%E7%B4%A2%E4%BA%8C%E7%BB%B4%E7%9F%A9%E9%98%B5.html" title="搜索二维矩阵 - 算法 - 二分查找">搜索二维矩阵 - 算法 - 二分查找</a><time datetime="2023-12-04T04:50:20.000Z" title="发表于 2023-12-04 12:50:20">2023-12-04</time></div></div></div></div></div></div></main><footer id="footer" style="background-image: url('https://api.aqcoder.cn/random')"><div id="footer-wrap"><div class="copyright">&copy;2023 - 2024 By Mr.wu</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox/fancybox.umd.min.js"></script><script src="https://cdn.jsdelivr.net/npm/vanilla-lazyload/dist/lazyload.iife.min.js"></script><div class="js-pjax"></div><canvas id="universe"></canvas>'<script src="/js/universe.js"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/activate-power-mode.min.js"></script><script>POWERMODE.colorful = true;
POWERMODE.shake = true;
POWERMODE.mobile = false;
document.body.addEventListener('input', POWERMODE);
</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div class="no-result" id="local-search-results"></div><div id="local-search-stats-wrap"></div></div></div><div id="search-mask"></div><script src="/js/search/local-search.js"></script></div></div><script async>window.onload=function(){var a=document.createElement('script'),b=document.getElementsByTagName('script')[0];a.type='text/javascript',a.async=!0,a.src='/sw-register.js?v='+Date.now(),b.parentNode.insertBefore(a,b)};</script></body></html>